package com.asdan.seededu.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;



import com.asdan.seededu.entity.security.SubProject;
import com.asdan.seededu.service.ProjectService;
import com.asdan.seededu.util.DatabaseAccess;


public class ProjectServiceImpl extends UtilImpl implements ProjectService {

	public List<SubProject> getProjects(String fpid,String spid) {
		// TODO Auto-generated method stub
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		List<SubProject> spList = new ArrayList<SubProject>()  ;
		String hql = "" ;
		if(fpid !=null&&!"".equals(fpid)){
			hql += " AND fpid = "+ fpid;
		}else{
			hql += " AND spid = "+ spid;	
		}
		
		String sql = "select spid , fpid , name , pcreatTime ,startTime ,endTime,tuid from subproject  where  1=1 ";
		try{
			cn = databaseAccess.getConn();
			pstmt = cn.prepareStatement(sql + hql);
			System.out.println(sql + hql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				
				SubProject sp= new SubProject();
				
  				sp.setSpid(rs.getInt("spid")) ;
  				sp.setFpid(rs.getInt("fpid")) ;
  				sp.setName(rs.getString("name")) ;
  				sp.setPcreatTime(rs.getInt("pcreatTime")) ;
  				sp.setStartTime(rs.getInt("startTime")) ;
  				sp.setEndTime(rs.getInt("endTime")) ;
  				sp.setTuid(rs.getInt("tuid")) ;
				
				spList.add(sp) ;
				
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			try{
				pstmt.close();
				cn.close();
				databaseAccess.close() ;
			}catch(Exception ex){
					pstmt = null ;
					cn = null ;
			}
		}
		return spList;
	}

	public boolean addProjects(String fpid, String spid) {
		// TODO Auto-generated method stub
		
		return false;
	}

}
